package com.fastwire.carriere.service;

import java.util.List;

import javax.jws.WebService;

import com.fastwire.carriere.common.Messages;
import com.fastwire.carriere.common.util.ResourceUtil;
import com.fastwire.carriere.controller.ControllerProxy;
import com.fastwire.carriere.controller.DiscoveryController;
import com.fastwire.carriere.controller.SchedulerController;
import com.fastwire.carriere.controller.ServiceOrderController;
import com.fastwire.carriere.controller.impl.DiscoveryControllerImpl;
import com.fastwire.carriere.controller.impl.SchedularControllerImpl;
import com.fastwire.carriere.controller.impl.ServiceOrderControllerImpl;
import com.fastwire.carriere.discovery.vo.DiscoveryRecordVo;
import com.fastwire.carriere.schedule.vo.ScheduledTaskVo;
import com.fastwire.carriere.service.dataconverter.XMLConverter;
import com.fastwire.carriere.service.webui.exception.ServiceException;
import com.fastwire.carriere.serviceorder.vo.OrderSearchCriteriaVo;
import com.fastwire.carriere.serviceorder.vo.ServiceOrderVo;
import com.fastwire.carriere.user.vo.SessionVo;

@WebService(endpointInterface = "com.fastwire.carriere.service.ActiveService")
public class ActiveServiceImpl implements ActiveService {

	@Override
	public String searchbyOrderSearchCriteria(OrderSearchCriteriaVo criteriaVo,
			SessionVo sessionVo) throws ServiceException {

		try {
			ServiceOrderController controller = (ServiceOrderController) ResourceUtil
					.getSessionBindClassByName(
							ServiceOrderControllerImpl.class.getName(),
							sessionVo, false);
			List<ServiceOrderVo> serviceOrderVos = controller
					.searchServiceOrder(criteriaVo);

			return XMLConverter.serilizeObjectListToXML(serviceOrderVos);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);

		}

	}

	@Override
	public String performManualEBOD(ServiceOrderVo serviceOrderVo,
			SessionVo sessionVo) throws ServiceException {
		String msg = Messages.getString("message.common.service.success");

		try {
			ServiceOrderController controller = (ServiceOrderController) ResourceUtil
					.getSessionBindClassByName(
							ServiceOrderControllerImpl.class.getName(),
							sessionVo, true);
			controller.performManualEBOD(serviceOrderVo);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);

		}

		return msg;

	}

	@Override
	public String createServiceOrder(ServiceOrderVo serviceOrderVo,
			SessionVo sessionVo) throws ServiceException {
		String msg = Messages.getString("message.common.service.success");

		try {
			ServiceOrderController controller = (ServiceOrderController) ResourceUtil
					.getSessionBindClassByName(
							ServiceOrderControllerImpl.class.getName(),
							sessionVo, false);
			controller.createServiceOrder(serviceOrderVo);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);

		}

		return msg;

	}

	@Override
	public String updateServiceOrder(ServiceOrderVo serviceOrderVo,
			SessionVo sessionVo) {
		String msg = Messages.getString("message.common.service.success");

		try {
			ServiceOrderController controller = (ServiceOrderController) ResourceUtil
					.getSessionBindClassByName(
							ServiceOrderControllerImpl.class.getName(),
							sessionVo, true);
			// serviceOrderVo.setLastUpdatedby(sessionVo.getUserName());
			controller.updateServiceOrder(serviceOrderVo);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Messages.getString("message.common.service.exception",
					new String[] { e.getMessage() });
		}

		return msg;

	}

	@Override
	public List<ScheduledTaskVo> getAllDiscoveryTasks(SessionVo sessionVo)
			throws ServiceException {

		try {
			SchedulerController controller = (SchedulerController) ResourceUtil
					.getSessionBindClassByName(
							SchedularControllerImpl.class.getName(), sessionVo,
							true);
			return controller.getAllDiscoveryTasks();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);

		}
	}

	@Override
	public String createDiscoveryTask(ScheduledTaskVo taskVo,
			SessionVo sessionVo) throws ServiceException {
		String msg = Messages.getString("message.common.service.success");

		try {
			SchedulerController controller = (SchedulerController) ResourceUtil
					.getSessionBindClassByName(
							SchedularControllerImpl.class.getName(), sessionVo,
							true);

			taskVo.setLastUpdateby(sessionVo.getUserName());
			controller.createU2kDiscoveryTask(taskVo);
		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

		return msg;
	}

	@Override
	public String createProvisioningTask(ScheduledTaskVo taskVo,
			SessionVo sessionVo) throws ServiceException {
		String msg = Messages.getString("message.common.service.success");

		try {
			SchedulerController controller = (SchedulerController) ResourceUtil
					.getSessionBindClassByName(
							SchedularControllerImpl.class.getName(), sessionVo,
							true);

			taskVo.setLastUpdateby(sessionVo.getUserName());
			controller.createU2kProvisioningTask(taskVo);
		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

		return msg;
	}

	@Override
	public String cancelScheduledDiscoveryTask(ScheduledTaskVo taskVo,
			SessionVo sessionVo) throws ServiceException {
		String msg = Messages.getString("message.common.service.success");

		try {
			SchedulerController controller = (SchedulerController) ResourceUtil
					.getSessionBindClassByName(
							SchedularControllerImpl.class.getName(), sessionVo,
							true);

			taskVo.setLastUpdateby(sessionVo.getUserName());
			controller.cancelU2kDiscoveryTask(taskVo);
			return msg;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

	}

	@Override
	public List<DiscoveryRecordVo> getDiscoveryRecordbySearchCriteria(
			DiscoveryRecordVo searchCriteraVo, SessionVo sessionVo)
			throws ServiceException {

		try {
			DiscoveryController controller = (DiscoveryController) ResourceUtil
					.getSessionBindClassByName(
							DiscoveryControllerImpl.class.getName(), sessionVo,
							true);
			return controller.searchDiscoveryRecord(searchCriteraVo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);

		}

	}
	
	@Override
	public String startScheduler(SessionVo sessionVo) throws ServiceException 
	{		
		String msg = Messages.getString("message.common.service.success");

		try 
		{
			SchedulerController controller = (SchedulerController) ResourceUtil
					.getSessionBindClassByName(
							SchedularControllerImpl.class.getName(), sessionVo,
							true);
			//taskVo.setLastUpdateby(sessionVo.getUserName());
			//controller.startScheduler();
			return msg;
		} 
		catch (Exception e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}
	}
	
}
